<?php
namespace app\admin\controller;

use app\common\model\OrderOperate;
use app\common\model\OrderProduct;
use think\Db;
use think\Request;
use app\admin\tool\Parser;
use app\common\model\Order as OrderModel;

/**
 * 财务对订单的管理
 * Class OrderAccountant
 * @package app\admin\controller
 */
class OrderAccountant extends Base{

    /**
     * 订单列表
     * 财务能查看所有的订单 但是只能对有编辑权限的订单进行状态修改
     * @param Request $request
     * @throws
     * @return \think\response\view
     */
    public function index(Request $request){
        $state = $request->get('state',0);
        $where = ['state'=>$state];
        $model = OrderModel::where($where)->order('update_time desc')->paginate(10);

        $this->data['title'] =  '订单列表';
        $this->data['model'] = $model;
        $this->data['parser'] = new Parser();
        $this->data['column'] = $this->column();
        $this->data['state'] = $state;

        //是否允许编辑
        $state_map =  get_order_state_accountant_map();
        $allow_edit_state_map = array_keys($state_map);
        $this-> data['allow_edit'] = false;
        if(in_array($state,$allow_edit_state_map)){
           $this-> data['allow_edit'] = true;
        }

        return view('',$this->data);
    }


    /**
     * 财务对订单进行状态的改变
     * @param Request $request
     * @throws
     * @return \think\response\view
     */
    public function edit(Request $request){
        $id = $request->param('id');
        $order = OrderModel::get($id);
        if(!$order){
            $this->error("非法操作");
        }

        $order->picture_list = json_decode($order->picture_list);
        $order_product = OrderProduct::where(['order_id'=>$order->id])->select();

        $this->data['model'] = $order;
        $this->data['order_product'] = $order_product;
        $this->data['title'] = '订单编辑';


        return view("",$this->data);
    }

    /**
     * 可以考虑在核对 收货到订单完成这个阶段 给个时间判断，也就是说至少要多少天财务才能 改变订单到 订单完成
     * 财务对订单的状态更新
     * 财务只能从状态 未审核
     * @param Request $request
     * @throws
     */
    public function update(Request $request){
        $post = $request->post();
        $order_map = get_order_state_accountant_map();
        $order = OrderModel::get($post['id']);
        if(!$order){
            $this->error('订单不存在');
        }

        if(!in_array(intval($post['state']),$order_map[$order->state])){
            $this->error("错误的操作 ");
        }

        Db::startTrans();
        try{
            $order->state = $post['state'];
            $order->save();
            OrderOperate::log($this->admin->id,$order->id,$post['state'],$post['message']);
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            $this->error("操作失败");
        }

        $this->success("操作成功",url('admin/order_accountant/index'));
    }



    public function column(){
        return [
            'order_num'=>['name'=>'订单编号','fun'=>'text','options'=>''],
            'total_price'=>['name'=>'总金额','fun'=>'text','options'=>''],
            'total_salary'=>['name'=>'总佣金','fun'=>'text','options'=>''],
            'cellphone' =>['name'=>'联系人手机','fun'=>'text','options'=>''],
            'add_time'=>['name'=>'添加时间','fun'=>'datetime','options'=>['format'=>'Y-m-d H:i']],
            'update_time'=>['name'=>'更新时间','fun'=>'text','options'=>[]],
        ];
    }

}